﻿using System;
using System.Text;
using System.ServiceModel;
using CustomSecurity;
using System.Xml;
using System.ServiceModel.Security;
using System.ServiceModel.Channels;

namespace Lib.CommonForm
{
    public class ProxyConfig
    {
        //Binding
        public static Binding GetBinding(string uri, bool needUserName)
        {
            WSHttpBinding wsHttpBinding = new WSHttpBinding();
            NetTcpBinding netTCPBinding = new NetTcpBinding();
            AssertEncryptionHttpBinding binding = new AssertEncryptionHttpBinding();

            TimeSpan timeSpan = new TimeSpan(0, 10, 0);
            if (uri.Contains("net.tcp://"))
            {
                netTCPBinding.Name = "Binding";
                //netTCPBinding.CloseTimeout = timeSpan;
                //netTCPBinding.OpenTimeout = timeSpan;
                //netTCPBinding.ReceiveTimeout = timeSpan;
                //netTCPBinding.SendTimeout = timeSpan;

                netTCPBinding.CloseTimeout = TimeSpan.FromMinutes(1);
                netTCPBinding.OpenTimeout = TimeSpan.FromMinutes(1);
                netTCPBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                netTCPBinding.SendTimeout = TimeSpan.FromMinutes(10);

                netTCPBinding.TransactionFlow = false;
                netTCPBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                netTCPBinding.MaxBufferPoolSize = 2147483647;
                netTCPBinding.MaxReceivedMessageSize = 2147483647;
                netTCPBinding.ReaderQuotas = new XmlDictionaryReaderQuotas();
                netTCPBinding.ReaderQuotas.MaxDepth = 32;
                netTCPBinding.ReaderQuotas.MaxStringContentLength = 8192;
                netTCPBinding.ReaderQuotas.MaxArrayLength = 16384;
                netTCPBinding.ReaderQuotas.MaxBytesPerRead = 4096;
                netTCPBinding.ReaderQuotas.MaxNameTableCharCount = 16384;
                netTCPBinding.Security.Mode = SecurityMode.None;
                netTCPBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
                netTCPBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
                netTCPBinding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
                return netTCPBinding;
            }
            else
            {
                if (needUserName)
                {
                    binding = new AssertEncryptionHttpBinding();
                    binding.Name = "Binding";

                    //binding.CloseTimeout = timeSpan;
                    //binding.OpenTimeout = timeSpan;
                    //binding.ReceiveTimeout = timeSpan;
                    //binding.SendTimeout = timeSpan;

                    binding.CloseTimeout = TimeSpan.FromMinutes(1);
                    binding.OpenTimeout = TimeSpan.FromMinutes(1);
                    binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                    binding.SendTimeout = TimeSpan.FromMinutes(10);

                    binding.MaxReceivedMessageSize = 2147483647;
                    binding.MessageEncoding = WSMessageEncoding.Text;
                    binding.MessageVersion = MessageVersion.Soap12;
                    binding.ReaderQuotas.MaxDepth = 32;
                    binding.ReaderQuotas.MaxStringContentLength = 2147483647;
                    binding.ReaderQuotas.MaxArrayLength = 2147483647;
                    binding.ReaderQuotas.MaxBytesPerRead = 2147483647;
                    binding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
                    binding.SecurityMode = AssertEncryptionHttpSecurityMode.UserNameOverMessage;
                    return binding;
                }
                else
                {
                    wsHttpBinding.Name = "Binding";
                    //wsHttpBinding.CloseTimeout = timeSpan;
                    //wsHttpBinding.OpenTimeout = timeSpan;
                    //wsHttpBinding.ReceiveTimeout = timeSpan;
                    //wsHttpBinding.SendTimeout = timeSpan;

                    wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1);
                    wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1);
                    wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                    wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(10);

                    wsHttpBinding.TransactionFlow = false;
                    wsHttpBinding.BypassProxyOnLocal = false;
                    wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                    wsHttpBinding.MaxBufferPoolSize = 2147483647;
                    wsHttpBinding.MaxReceivedMessageSize = 2147483647;
                    wsHttpBinding.MessageEncoding = WSMessageEncoding.Text;
                    wsHttpBinding.TextEncoding = Encoding.UTF8;
                    wsHttpBinding.UseDefaultWebProxy = true;
                    wsHttpBinding.ReaderQuotas = new XmlDictionaryReaderQuotas();
                    wsHttpBinding.ReaderQuotas.MaxDepth = 32;
                    wsHttpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
                    wsHttpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
                    wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
                    wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
                    wsHttpBinding.Security.Mode = SecurityMode.None;
                    wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                    wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                    wsHttpBinding.Security.Transport.Realm = string.Empty;
                    wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
                    wsHttpBinding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
                    wsHttpBinding.Security.Message.NegotiateServiceCredential = false;

                    return wsHttpBinding;
                }
            }

        }

    }
}
